package cn.yitun.chat.yitunserverfilecore.controller;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunCollectionUtil;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunservercommonlib.basebean.web.JwtUserDto;
import cn.yitun.chat.yitunservercommonlib.converter.ot.file.GroupHeadDataPoToBaseConverter;
import cn.yitun.chat.yitunservercommonlib.converter.ot.file.UserHeadDataPoToBaseConverter;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.BaseController;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.group.GroupHeadDataInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.userchat.UserHeadDataInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.group.GroupHeadDataVo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.userchat.UserHeadDataVo;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunserverfilecore.component.DownloadFileHandler;
import cn.yitun.chat.yitunserverfilecore.service.FileServerConfigService;
import cn.yitun.chat.yitunserverfilecore.service.HeadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 头像上传下载接口
 * @author yitun
 * @date 2017-11-25 09:30:45
 *
 */
@Controller
@RequestMapping("/file/head")
public class HeadUploadController extends BaseController {

	@Autowired
	private HeadService headService;
    @Resource
    FileServerConfigService fileServerConfigService;
	DownloadFileHandler dfh = new DownloadFileHandler();


	@CrossOrigin
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST,value ="/user/upload")
	public Object userUpload(JwtUserDto jwtUserDto, HttpServletRequest request) {
		ResultMessage rm = new ResultMessage();
		Long userId = jwtUserDto.getUserId() ;
		Enumeration e =  request.getParameterNames();
		List<UserHeadDataVo> list = headService.userUploadByIO(request, userId);
		Map<String, Object> dataMap = new HashMap<>();
		if (null != list && !list.isEmpty()) {
			UserHeadDataVo data = list.get(0);
			String name = data.getName();
			String saveName = data.getSaveName();
			String nodePath = data.getNodePath();

			dataMap.put("id", data.getId());
			dataMap.put("name", name);
			dataMap.put("size", data.getSize());
            dataMap.put("url", fileServerConfigService.getFileUrl(nodePath + saveName));
		}
		rm.bodyPut("data", dataMap);
		return rm;
	}

	@RequestMapping("/user/download")
	public void userDownload(Long id, HttpServletRequest request, HttpServletResponse response) {
		if (id != null) {
			UserHeadDataInfo fd = headService.getUserHeadData(id);
			if (null != fd) {
				dfh.downloadByIO(request, response, UserHeadDataPoToBaseConverter.INSTANCE.po2base(fd));
			}
		}
	}
	
	
	
	@CrossOrigin
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST,value ="/group/upload")
	public Object upload(JwtUserDto userDto, Long groupId, HttpServletRequest request, HttpServletResponse response) {
		ResultMessage rm = new ResultMessage();
		Long userId = userDto.getUserId() ;
		if (NumUtil.isInValid(groupId)) {
			rm.addError(ExceptionEnum.LOSE_FILED);
			return rm;
		}
		List<GroupHeadDataVo> list = headService.groupUploadByIO(request, userId,  groupId);
		Map<String, Object> dataMap = new HashMap<>();
		if (YitunCollectionUtil.isNotEmpty(list)) {
			GroupHeadDataVo headData = list.get(0);
			String name = headData.getName();
			String saveName = headData.getSaveName();
			String nodePath = headData.getNodePath();

			dataMap.put("id", headData.getId());
			dataMap.put("name", name);
			dataMap.put("size", headData.getSize());
            dataMap.put("url", fileServerConfigService.getFileUrl(nodePath + saveName));
		}
		rm.bodyPut("data", dataMap);
		return rm;
	}

	@CrossOrigin
	@RequestMapping(method = RequestMethod.POST,value ="/group/download")
	public void download(Long id, HttpServletRequest request, HttpServletResponse response) {
		if (id != null) {
			GroupHeadDataInfo fd = headService.getGroupHeadData(id);
			if (null != fd) {
				dfh.downloadByIO(request, response, GroupHeadDataPoToBaseConverter.INSTANCE.po2base(fd));
			}
		}
	}
}
